/*
   file : mmdb.c
    词法、语法分析部分所要用到的一些函数
   University of Jiangsu
   memoryDB      2010.6
 */



#include "mmdb.h"

void Init_sql(sql_sentence *sql)
{
	sql->select_sentence=NULL;
	sql->create_sentence=NULL;
	sql->update_sentence=NULL;
	sql->delete_sentence=NULL;
	sql->insect_sentence=NULL;
	sql->drop_sentence=NULL;
	sql->where_struct=NULL;
	sql->table_struct=NULL;
	sql->cur_table_field=NULL;
	bzero(sql->str,MAX_COMMAND);	
}

void Init_Field(Field *f)
{
	f->Next_Field=NULL;
	strcpy(f->Field_Name ,"");
	strcpy(f->Table_Name ,"");
}

void Init_Table(Table *t)
{
	t->Field_list = NULL;
	t->Field_num =0;
	t->Last_Field = NULL;
	t->Next_Tab  = NULL;
	strcpy(t->Table_Name , "");
}

void Init_Record(Record *r)
{
	r->Last_field = NULL;
	r->Field_list = NULL;
	r->Feild_num = 0;
	r->Next_record = NULL;
}

void Init_Row(Row *r)
{
	r->next_row = NULL;
}

void Init_Atom(Atom *a)
{
	a->Atom_field= NULL;
}

void Init_Where_node(Where_node *wn)
{
	strcpy(wn->Table_name,"");
	wn->Next_where_node =NULL;
	wn->R_atom = NULL;
	wn->L_atom = NULL;
}

void Init_WhereTable(WhereTable *wt)
{
	wt->Where_node_num = 0;
	wt->last_node = NULL;
	strcpy(wt->name , "");
	wt->Next_where_tab = NULL;
	wt->Where_node_list = NULL;
}

void Init_Where(Where *w)
{
	w->Where_table_list = NULL;
	w->where_type = ORX;

}

void Init_Select(Select *s)
{
	s->Select_list = NULL;
	s->Select_where = NULL;
}

void Init_Create_Table(Create_Table *ct)
{
	ct->Tab = NULL;
}

void Init_Update(Update *u)
{
	u->Select_where = NULL;
	u->Tab  =NULL;
}

void Init_Insect(Insect *i)
{
	i->Tab = NULL;
}

void Init_Delete(Delete *d)
{
	d->Select_where = NULL;
        d->Select_Table = NULL;
}

void Init_Drop_Table(Drop_Table *d)
{
	strcpy(d->name , "");
}

void SetVal(Field *f,int i)
{
	f->date_type = INTX;
	f->Int_Val = i;
}

void SetDoubVal(Field *f,double d)
{
	f->date_type = DOUBLEX;
	f->Double_Val = d;
}

void SetStrVal(Field *f,char *str,int strlen)
{
	f->date_type = STRINGX;
	strcpy(f->Str_Val , str);
	f->str_len = strlen;

}

int GetIntVal(Field *f)
{
	return f->Int_Val;
}

double GetDoubVal(Field *f)
{
	return f->Double_Val;
}

char *GetStrVal(Field *f)
{
	return f->Str_Val;
}

void SetDateType(Field *f,enum Date_type dt)
{
	f->date_type = dt;
}

int AddField(Table *T,Field *fid)
{
	if(!T || !fid)
		return 0;
	if(!T->Field_list)
	{
		T->Field_list = fid;
		T->Field_num = 1;
	}
	else
	{
		T->Last_Field->Next_Field = fid;
		T->Field_num++;
	}
        T->Last_Field = fid;
//	printf("finish AddField\n");
	return 1;

}

Field *GetField(Table *T,char* name)
{
	struct Field* p;
	if(!T->Field_list)return NULL;
	p = T->Field_list;
	for(;p!=NULL;p = p->Next_Field)
	{
		if(strcmp(p->Field_Name,name))
		{
			return p;
		}
	}
	return NULL;
}

int AddField_Record(Field *f,Record *r)
{
	if(!r||!f)return 0;
	if(!r->Field_list)
		r->Field_list = f;
	else
		r->Last_field->Next_Field = f;
	r->Last_field = f;
	r->Feild_num++;
	return 1;

}

void error(char *s,int sockfd)
{
	char newline[MAX_COMMAND];
	sprintf(newline,"%s",s);
	if(sockfd != 1)
	  strcat(newline,"\n\n\n");
	write(sockfd,newline,strlen(newline));
}

void Free_Field(Field *f)
{
	free(f);
}

void Free_Table(Table *t)
{
	if(t->Field_list)
	{
		Field *cf = t->Field_list;
        Field *nf = cf->Next_Field;
		for(;cf;)
		{
			Free_Field(cf);
			cf = nf;
			if(nf)
				nf=nf->Next_Field;
		}
	}
	free(t);
}
void Free_Record(Record *record)
{
   if(!record)
	return;


      Field *f = record->Field_list;
      Field *pf;
      while(f)
      {
         pf = f;
         f=f->Next_Field;
         free(pf);
      }
	free(record);
}
void Free_WhereTable(WhereTable *wt)
{
	if(wt->Where_node_list)
	{
		Where_node* cwn = wt->Where_node_list;
        Where_node* nwn = cwn->Next_where_node;
		for(;cwn;)
		{
			free(cwn);
			cwn = nwn;
			if(nwn)
				nwn = nwn->Next_where_node;
		}
	}
}

void Free_Where(Where *w)
{
	if(w->Where_table_list)
	{
		WhereTable* cwt = w->Where_table_list;
        WhereTable* nwt = cwt->Next_where_tab;
		for(;cwt;)
		{
			Free_WhereTable(cwt);
			cwt = nwt;
			if(nwt)
				nwt = nwt->Next_where_tab;
		}
	}
	free(w);
}

void Free_Select(Select *s)
{
	if(s->Select_list)
		Free_Table(s->Select_list);
	if(s->Select_where)
		Free_Where(s->Select_where);
	free(s);

}

void Free_Create(Create_Table *c)
{
	if(c->Tab)
		Free_Table(c->Tab);
	free(c);
}

void Free_Update(Update *u)
{
	if(u->Tab)
		Free_Table(u->Tab);
	if(u->Select_where)
		Free_Where(u->Select_where);
	free(u);

}

void Free_Insect(Insect *i)
{
	if(i->Tab)
		Free_Table(i->Tab);
	free(i);
}

void Free_Delete(Delete *d)
{
	if(d->Select_where)
		Free_Where(d->Select_where);
	free(d);
}

void Free_Drop(Drop_Table *d)
{
	free(d);
}
